package com.jf.mall.domain;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-09-29
 * Time:16:01
 */
public class Page<T> {
    //默认一页显示记录数
    public static final int DEFAULT_PAGE_SIZE = 4;
    //默认页码导航条显示页数
    private static final int DEFAULT_BAR_SIZE = 3;
    //当前页码
    private int pageNo;
    //一页显示记录数
    private int pageSize;
    //总页码数
    private int pageCount;
    //元素在数据库的起始下标
    private int elementBegin;
    //元素个数
    private int elementsCount;
    //元素集合
    private List<T> elements;
    //页码导航条的起始页码
    private int beginIndex;
    //页码导航条的结束页码
    private int endIndex;
    //当前页的url
    private String url;

    public Page(int pageNo, int pageSize) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        elementBegin = (pageNo - 1) * pageSize;
        url = "";
    }

    public void setElementsCount(int elementsCount) {
        this.elementsCount = elementsCount;
        int count = elementsCount / pageSize;
        pageCount = elementsCount % pageSize == 0 ? count : count + 1;
        int front = DEFAULT_BAR_SIZE / 2;
        int behind = DEFAULT_BAR_SIZE - front - 1;
        beginIndex = pageNo - front;
        endIndex = pageNo + behind;
        if (beginIndex < 1) {
            beginIndex = 1;
            endIndex = beginIndex + DEFAULT_PAGE_SIZE;
        }
        if (endIndex > pageCount) {
            endIndex = pageCount;
        }

    }

    public void setElements(List<T> elements) {
        this.elements = elements;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<T> getElements() {
        return elements;
    }

    public int getPageNo() {
        return pageNo;
    }

    public int getPageCount() {
        return pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getBeginIndex() {
        return beginIndex;
    }

    public int getElementsCount() {
        return elementsCount;
    }

    public int getElementBegin() {
        return elementBegin;
    }

    public int getEndIndex() {
        return endIndex;
    }

    public String getUrl() {
        return url;
    }
}
